package com.ljf.framework.Async;


import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 说明：自定义线程池
 *
 * @Auther: lijinfeng
 * @Date: 2024/8/1
 */
public class ThreadPoolConfiguration {

    private static ThreadPoolExecutor systemPoolExecutorService;
    private static ThreadPoolExecutor dbPoolExecutorService;

    static  {
        setSystemPoolExecutorService();
        setDbPoolExecutorService();
    }

    /**
     * 系统异步任务线程池
     *
     * @return
     */
    public static void setSystemPoolExecutorService() {

        ThreadPoolConfiguration.systemPoolExecutorService = new ThreadPoolExecutor(3,
                10,
                60,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(100),
                Executors.defaultThreadFactory(),
                (r, executor) -> System.out.println("system pool is full! ")
        );
    }

    /**
     * 数据库异步任务线程池
     *
     * @return
     */
    public static void setDbPoolExecutorService() {
        ThreadPoolConfiguration.dbPoolExecutorService = new ThreadPoolExecutor(3,
                10,
                60,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(100),
                Executors.defaultThreadFactory(),
                (r, executor) -> System.out.println("system pool is full! ")
        );
    }

    public static ThreadPoolExecutor getSystemPoolExecutorService() {
        return systemPoolExecutorService;
    }

    public static ThreadPoolExecutor getDbPoolExecutorService() {
        return dbPoolExecutorService;
    }
}
